package com.webbei.Backstage.action;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.wabbei.friendcenter.action.ReNameUtil;
import com.webbei.Backstage.service.BackstateSer;
import com.webbei.pojo.Carseries;
import com.webbei.pojo.Cartypes;


public class UpLoadBrandAction {
	private BackstateSer bao;
	private Integer brand;
	private File swfupload;
	private String newCarName;
	private String swfuploadFileName;
	public String execute(){
		String path=ServletActionContext.getServletContext().getContextPath();
		System.out.println(path);
		int count=bao.chackTheNewCarName(newCarName);
		if(count==0){
			Carseries car =new Carseries();
			car.setBrandsId(brand);
			car.setSeriesName(newCarName);
			int i=bao.insertNewCarseries(car);
			if(i==1){
				System.out.println(newCarName);
				int sid=bao.getCarseriesIdBySeriesName(newCarName);
				System.out.println("sid:"+sid);
				String ext =fileExt(swfuploadFileName);
				String NewNameFile=ReNameUtil.GetNewName()+ext;
				System.out.println(NewNameFile);
				copyFile(swfupload,NewNameFile);//send file
				Cartypes cartype=new Cartypes();
				cartype.setSeriesId(sid);
				cartype.setTypesPic(NewNameFile);
				cartype.setTypesName(swfuploadFileName);
				int j=bao.insertNewCarTypes(cartype);
				if(j==1){
					System.out.println("insert CarTypes success");
				}else{
					System.out.println("insert CarTypes fails");
					return null;
				}
				System.out.println("insert is ok");
			}else{
				System.out.println("insert Carseries fails");
				return null;
			}
		}else{
			int sid=bao.getCarseriesIdBySeriesName(newCarName);
			System.out.println("sid:"+sid);
			String ext =fileExt(swfuploadFileName);
			String NewNameFile=ReNameUtil.GetNewName()+ext;
			System.out.println(NewNameFile);
			copyFile(swfupload,NewNameFile);//send file
			Cartypes cartype=new Cartypes();
			cartype.setSeriesId(sid);
			cartype.setTypesPic(NewNameFile);
			cartype.setTypesName(NewNameFile);
			int j=bao.insertNewCarTypes(cartype);
			if(j==1){
				System.out.println("insert CarTypes success");
			}else{
				System.out.println("insert CarTypes fails");
				return null;
			}
			System.out.println("insert is ok");
		}
		return null;
	}
	
	public String getSwfuploadFileName() {
		return swfuploadFileName;
	}
	public void setSwfuploadFileName(String swfuploadFileName) {
		this.swfuploadFileName = swfuploadFileName;
	}
	public String getNewCarName() {
		return newCarName;
	}
	public void setNewCarName(String newCarName) {
		this.newCarName = newCarName;
	}
	public File getSwfupload() {
		return swfupload;
	}
	public void setSwfupload(File swfupload) {
		this.swfupload = swfupload;
	}
	public Integer getBrand() {
		return brand;
	}
	public void setBrand(Integer brand) {
		this.brand = brand;
	}
	public BackstateSer getBao() {
		return bao;
	}
	public void setBao(BackstateSer bao) {
		this.bao = bao;
	}
	public String fileExt(String fileName){
		String ext=fileName.substring(fileName.indexOf("."));
		return ext;
	}
	public boolean copyFile(File f,String fName){
		boolean b=true;
		if(f!=null){
				String realPath=ServletActionContext.getServletContext().getRealPath("/CarMark");
				System.out.println(realPath);
				File newFile=new File(realPath);
				if(!newFile.exists()){
					newFile.mkdirs();
				}
				try {
					FileUtils.copyFile(f,new File(newFile,fName));
				} catch (IOException e) {
					e.printStackTrace();
				}
				System.out.println("send file successful");
		}else{
			b=false;
		}
		return b;
	}
}
